<template>
	<view class="container">
		<!-- 导航栏 -->
		 <u-navbar
		 titleStyle ="color:#FFFFFF;font-size: 30rpx;"
		 leftIconColor="#FFFFFF"
		 bgColor="#177A77"
		            title="采购入库"
		            @rightClick="rightClick"
		            :autoBack="true"
		        >
		        </u-navbar>
		<!-- 搜索 -->
		<view class="search">
			<view class="v1">
				<image style="width:23rpx;height: 23rpx;" src="/static/work/ty_ss02.png" mode=""/>
				  <input
				  @confirm="inputConfirm"
				  type="text" 
				  v-model="keyword" 
				  placeholder="搜索采购单号" 
				  style="font-size: 24rpx;margin-left: 15rpx;"
				  placeholder-style="color:#FFFFFF;opacity: 1;font-size:24rpx;"
				  />
			</view>
		</view>
		<!-- 筛选项 -->
		<view class="all">
			<u-tabs  :list="tabs" @click="clickTabs" lineColor="#FFFFFF" inactiveStyle="opacity: 0.5;color:#FFF;font-size:26rpx" activeStyle="color:#FFF;font-size:26rpx"></u-tabs>
			
		</view>
		<!-- 信息 -->
		<view class="information">
			<scroll-view  scroll-y="true" class="scroll-Y" 
				@scrolltolower="lower" @refresherrefresh="onRefresh" refresher-enabled="true" :refresher-triggered="triggered">
				<view class="block" v-for="(item,index) in list" :key="index">
					<view class="blockTop">
						<view style="color: #333333;">采购单号：<span class="spans">{{item.no}}</span></view>
						<view style="color: #FF6633;">{{calculationStatus(item.items)}}</view>
					</view>
					<view class="blockBottom" @click="handeleDetail(item)">
						<view class="bottomLeft">
							<view>供应商：<span class="spans">{{item.supplierName}}</span></view>
							<view class="bottomCen">
								<view style="width: 39%;">入库类型：<span class="spans">采购入库</span></view>
								<!-- <view>货品种类：<span class="spans">5</span></view> -->
								<!-- <view style="margin-left: 20rpx;">存储仓库：<span class="spans">三号库</span></view> -->
							</view>
							<view class="bottomCen">
								<view style="width: 39%;">采购人员：<span class="spans">{{item.creatorName}}</span></view>
								<view>采购日期：<span class="spans">{{setTimes(item.orderTime)}}</span></view>
							</view>
						</view>
						<view class="icon">
							<image class="imageIcon" src="/static/work/ty_getinto02.png" mode=""></image>
						</view>
					</view>
				</view>
			</scroll-view>
		
		</view>
		<!-- 顶部背景图 -->
		<view class="header"></view>
	</view>
</template>

<script>
	import {purchaseOrder} from '@/api/store.js'
	import {setTimes} from '@/utils/common.js'
	export default {
		data() {
			return {
				placeholderColor:'#FFFFFF',
				indexID: 0, //默认起始下标
				tabs: [
					{name:'待入库'}, 
					// {name:'已入库'}, 
					// {name:'全部'},
				],//筛选条件
				col: '#4B9C6D',//搜索框背景色
				keyword: '',//搜索信息
				list:[],//数据
				total:0,//总条数
				queryParams:{
					pageNo: 1,//页码
					pageSize: 10,//条数
					inEnable:true,
					no:''
				},
				triggered:false,//下拉刷新
			}
		},
		onLoad() {
			this.handleCustomEventBind = this.handleCustomEvent.bind(this);
			uni.$on('getList', this.handleCustomEventBind);
			this.getData()
		},
		
		methods: {
			// 获取数据
			async getData (){
				let response =await purchaseOrder(this.queryParams)
				this.total = response.data.total
				this.list = [...this.list,...response.data.list]
				setTimeout(()=>{
					this.triggered = false
				},500)
			},
			rightClick(){
				
			},
			handleCustomEvent(){
				this.list=[]
				this.getData()
			},
			inputConfirm(){
				this.list = []
				this.queryParams.pageNo = 1
				this.queryParams.no = this.keyword
				this.getData()
			},
			// 时间戳转换
			setTimes(times){
				return setTimes(times)
			},
			// 触底加载数据
			lower(){
				// 当前数据的条数不等于总条数加载数据
				if(this.list.length<this.total){
					this.queryParams.pageNo++
					this.getData()
				}
			},
			onRefresh(){
				this.queryParams.pageNo=1
				this.triggered = true
				this.list=[]
				this.getData()
			},
			// 点击的 待入库 已入库 全部 TEXT
			handeleIndex(index) {
				this.indexID = index
			},
			// 搜索
			handeleSearch() {
				console.log('搜索');
			},
			// 返回上一页
			handeleGo() {
				uni.navigateBack()
			},
			// 入库详情
			handeleDetail(item) {
				uni.navigateTo({
					url: '/pages/work/store/storeDetail?items='+JSON.stringify(item)
				})
			},
			clickTabs(e){
				this.queryParams.pageNo=1
				this.list=[]
				// inStatus=0 未入库
				// inStatus=2  全部入库
				// inStatus=1  已入库
				if(e.name=='待入库'){
					this.queryParams.inEnable=true
					this.getData()
				}
				// if(e.name=='已入库'){
				// 	this.queryParams.inStatus = 1
				// 	this.getData()
				// }
				if(e.name=='全部'){
					this.queryParams.inEnable=false
					this.getData()
				}
			},
			// 计算当前入库状态
			calculationStatus(items){
				// 使用 reduce 计算 price 字段的合计
				const count = items.reduce((sum, item) => {
				  return sum + item.count;
				}, 0); // 初始值为 0
				
				const inCount = items.reduce((sum, item) => {
				  return sum + item.inCount;
				}, 0); // 初始值为 0
				
				if(inCount==0){
					return '待入库'
				}
				if(inCount<count){
					return '部分入库'
				}
				if(inCount==count){
					return '入库完成'
				}
			}
		},
		onUnload(){
			uni.$off('getList')
		}
	}
</script>

<style scoped>
	.v1{
		width: 656rpx;
		height: 70rpx;
		background: rgba(255, 255, 255, 0.5);
		border-radius: 33rpx;
		
		display: flex;
		align-items: center;
		padding: 0 31rpx;
		box-sizing: border-box;
		z-index: 3;
	}
	.v1 image {
	    width: 30rpx;
	    height: 30rpx;
	    /* 确保图标不受透明度影响，直接去掉透明度或给它单独设置样式 */
	    opacity: 1; /* 使图标完全不透明 */
	}

	.scroll-Y {
		width: 712rpx;
		height: calc(100vh - 500rpx);
	}

	.spans {
		color: #999999;
	}

	.bottomCen {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.bottomLeft {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		font-size: 24rpx;
	}

	.imageIcon {
		width: 24rpx;
		height: 24rpx;
	}

	.icon {
		width: 78rpx;
		height: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.blockBottom {
		width: 100%;
		height: 211rpx;
		display: flex;
		justify-content: space-between;
		padding: 42rpx 0rpx 44rpx 29rpx;
		box-sizing: border-box;
	}

	.blockTop {
		width: 100%;
		height: 89rpx;
		border-bottom: 1rpx solid #EBEBEB;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 32rpx;
		box-sizing: border-box;
		font-size: 24rpx;
	}

	.block {
		width: 705rpx;
		height: 301rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		border: 1px solid #EBEBEB;
		margin-bottom: 17rpx;
	}

	.information {
		width: 750rpx;
		height: calc(100vh - 420rpx);
		background: #ffffff;
		border-radius: 30rpx;
		margin-top: 30rpx;
		z-index: 2;
		padding: 60rpx 20rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
		/* overflow-y: auto; */
	}

	.lines {
		width: 36rpx;
		height: 6rpx;
		background: #FFFFFF;
		border-radius: 5rpx;
	}

	.line {
		width: 56rpx;
		height: 6rpx;
		background: #FFFFFF;
		border-radius: 5rpx;
	}

	.all {
		width: 608rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		z-index: 2;
		margin-top: 59rpx;
		margin-bottom: 20rpx;
		margin-left: -50rpx;
	}

	.cative {
		width: 608rpx;
		height: 50rpx;
		font-weight: 400;
		font-size: 26rpx;
		color: #FFFFFF;
		line-height: 20rpx;
		margin-right: 107rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}

	.activeo {
		width: 608rpx;
		height: 50rpx;
		font-weight: 400;
		font-size: 26rpx;
		color: #FFFFFF;
		line-height: 20rpx;
		margin-right: 107rpx;
		opacity: 0.5;
		/* 50% 透明度 */
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
	}

	.search {
		margin-top: 190rpx;
		z-index: 10;
		width: 656rpx;
		height: 62rpx;
	}

	.nvaImage {
		width: 38rpx;
		height: 38rpx;
	}

	.navbar {
		width: 654rpx;
		height: 62rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-weight: 400;
		font-size: 30rpx;
		color: #FFFFFF;
		margin-top: 38rpx;
		z-index: 2;
	}

	.container {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.header {
		width: 750rpx;
		height: 644rpx;
		background-color: #177A77;
		/* background: linear-gradient(0deg, #177A77, #4B9C6D); */
		position: absolute;
		top: 0;
		z-index: 1;
		left: 0;
	}
</style>